using System;

using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Hardware;

namespace Project
{
    class AddLightRulesWindow:Window
    {
        private Window addLightRulesWindow;
        Canvas canvas = new Canvas();
        private Text text_time;
        private int min = 0;
        private int hours = 12;
        Window prevwindow;
        SimpleButton button_kitchen_ticked;
        SimpleButton button_kitchen_unticked;
        SimpleButton button_living_ticked;
        SimpleButton button_living_unticked;
        SimpleButton button_bedroom_ticked;
        SimpleButton button_bedroom_unticked;
        SimpleButton button_bathroom_ticked;
        SimpleButton button_bathroom_unticked;
        SimpleButton button_state_on;
        SimpleButton button_state_off;

        bool kitchen = false;
        bool living = false;
        bool bedroom = false;
        bool bathroom = false;
        bool state = false;

        public AddLightRulesWindow(Window lsw)
        {
            prevwindow = lsw;
            addLightRulesWindow = new Window();
            addLightRulesWindow.Width = SystemMetrics.ScreenWidth;
            addLightRulesWindow.Height = SystemMetrics.ScreenHeight;

            CreateWindow();
        }

        private void CreateWindow()
        {
            Text text_kitchen = new Text();
            text_kitchen.Font = Resources.GetFont(Resources.FontResources.NinaB);
            text_kitchen.TextContent = "Kitchen";
            text_kitchen.HorizontalAlignment = Microsoft.SPOT.Presentation.HorizontalAlignment.Center;
            text_kitchen.VerticalAlignment = Microsoft.SPOT.Presentation.VerticalAlignment.Center;
            Panel pan_kitchen = new Panel();
            pan_kitchen.Width = addLightRulesWindow.Width / 2;
            pan_kitchen.Height = addLightRulesWindow.Height / 6;
            pan_kitchen.Children.Add(text_kitchen);

            Text text_living = new Text();
            text_living.Font = Resources.GetFont(Resources.FontResources.NinaB);
            text_living.TextContent = "Living Room";
            text_living.HorizontalAlignment = Microsoft.SPOT.Presentation.HorizontalAlignment.Center;
            text_living.VerticalAlignment = Microsoft.SPOT.Presentation.VerticalAlignment.Center;
            Panel pan_living = new Panel();
            pan_living.Width = addLightRulesWindow.Width / 2;
            pan_living.Height = addLightRulesWindow.Height / 6;
            pan_living.Children.Add(text_living);

            Text text_bedroom = new Text();
            text_bedroom.Font = Resources.GetFont(Resources.FontResources.NinaB);
            text_bedroom.TextContent = "Bedroom";
            text_bedroom.HorizontalAlignment = Microsoft.SPOT.Presentation.HorizontalAlignment.Center;
            text_bedroom.VerticalAlignment = Microsoft.SPOT.Presentation.VerticalAlignment.Center;
            Panel pan_bedroom = new Panel();
            pan_bedroom.Width = addLightRulesWindow.Width / 2;
            pan_bedroom.Height = addLightRulesWindow.Height / 6;
            pan_bedroom.Children.Add(text_bedroom);

            Text text_bathroom = new Text();
            text_bathroom.Font = Resources.GetFont(Resources.FontResources.NinaB);
            text_bathroom.TextContent = "Bathroom";
            text_bathroom.HorizontalAlignment = Microsoft.SPOT.Presentation.HorizontalAlignment.Center;
            text_bathroom.VerticalAlignment = Microsoft.SPOT.Presentation.VerticalAlignment.Center;
            Panel pan_bathroom = new Panel();
            pan_bathroom.Width = addLightRulesWindow.Width / 2;
            pan_bathroom.Height = addLightRulesWindow.Height / 6;
            pan_bathroom.Children.Add(text_bathroom);

            Image image_kitchen_ticked = new Image(Resources.GetBitmap(Resources.BitmapResources.ticked));
            image_kitchen_ticked.HorizontalAlignment = HorizontalAlignment.Center;
            image_kitchen_ticked.VerticalAlignment = VerticalAlignment.Center;
            button_kitchen_ticked = new SimpleButton(image_kitchen_ticked, addLightRulesWindow.Width / 4, addLightRulesWindow.Height / 6);
            button_kitchen_ticked.BackColor = Colors.White;

            Image image_kitchen_unticked = new Image(Resources.GetBitmap(Resources.BitmapResources.unticked));
            image_kitchen_unticked.HorizontalAlignment = HorizontalAlignment.Center;
            image_kitchen_unticked.VerticalAlignment = VerticalAlignment.Center;
            button_kitchen_unticked = new SimpleButton(image_kitchen_unticked, addLightRulesWindow.Width / 4, addLightRulesWindow.Height / 6);
            button_kitchen_unticked.BackColor = Colors.White;

            Image image_living_ticked = new Image(Resources.GetBitmap(Resources.BitmapResources.ticked));
            image_living_ticked.HorizontalAlignment = HorizontalAlignment.Center;
            image_living_ticked.VerticalAlignment = VerticalAlignment.Center;
            button_living_ticked = new SimpleButton(image_living_ticked, addLightRulesWindow.Width / 4, addLightRulesWindow.Height / 6);
            button_living_ticked.BackColor = Colors.White;

            Image image_living_unticked = new Image(Resources.GetBitmap(Resources.BitmapResources.unticked));
            image_living_unticked.HorizontalAlignment = HorizontalAlignment.Center;
            image_living_unticked.VerticalAlignment = VerticalAlignment.Center;
            button_living_unticked = new SimpleButton(image_living_unticked, addLightRulesWindow.Width / 4, addLightRulesWindow.Height / 6);
            button_living_unticked.BackColor = Colors.White;

            Image image_bedroom_ticked = new Image(Resources.GetBitmap(Resources.BitmapResources.ticked));
            image_bedroom_ticked.HorizontalAlignment = HorizontalAlignment.Center;
            image_bedroom_ticked.VerticalAlignment = VerticalAlignment.Center;
            button_bedroom_ticked = new SimpleButton(image_bedroom_ticked, addLightRulesWindow.Width / 4, addLightRulesWindow.Height / 6);
            button_bedroom_ticked.BackColor = Colors.White;

            Image image_bedroom_unticked = new Image(Resources.GetBitmap(Resources.BitmapResources.unticked));
            image_bedroom_unticked.HorizontalAlignment = HorizontalAlignment.Center;
            image_bedroom_unticked.VerticalAlignment = VerticalAlignment.Center;
            button_bedroom_unticked = new SimpleButton(image_bedroom_unticked, addLightRulesWindow.Width / 4, addLightRulesWindow.Height / 6);
            button_bedroom_unticked.BackColor = Colors.White;

            Image image_bathroom_ticked = new Image(Resources.GetBitmap(Resources.BitmapResources.ticked));
            image_bathroom_ticked.HorizontalAlignment = HorizontalAlignment.Center;
            image_bathroom_ticked.VerticalAlignment = VerticalAlignment.Center;
            button_bathroom_ticked = new SimpleButton(image_bathroom_ticked, addLightRulesWindow.Width / 4, addLightRulesWindow.Height / 6);
            button_bathroom_ticked.BackColor = Colors.White;

            Image image_bathroom_unticked = new Image(Resources.GetBitmap(Resources.BitmapResources.unticked));
            image_bathroom_unticked.HorizontalAlignment = HorizontalAlignment.Center;
            image_bathroom_unticked.VerticalAlignment = VerticalAlignment.Center;
            button_bathroom_unticked = new SimpleButton(image_bathroom_unticked, addLightRulesWindow.Width / 4, addLightRulesWindow.Height / 6);
            button_bathroom_unticked.BackColor = Colors.White;

            Image image_state_on = new Image(Resources.GetBitmap(Resources.BitmapResources.switch_on));
            image_state_on.HorizontalAlignment = HorizontalAlignment.Center;
            image_state_on.VerticalAlignment = VerticalAlignment.Center;
            button_state_on = new SimpleButton(image_state_on, addLightRulesWindow.Width / 4, addLightRulesWindow.Height / 6);
            button_state_on.BackColor = Colors.White;

            Image image_state_off = new Image(Resources.GetBitmap(Resources.BitmapResources.switch_off));
            image_state_off.HorizontalAlignment = HorizontalAlignment.Center;
            image_state_off.VerticalAlignment = VerticalAlignment.Center;
            button_state_off = new SimpleButton(image_state_off, addLightRulesWindow.Width / 4, addLightRulesWindow.Height / 6);
            button_state_off.BackColor = Colors.White;

            Canvas.SetTop(pan_kitchen, 0);
            Canvas.SetLeft(pan_kitchen, 0);
            canvas.Children.Add(pan_kitchen);

            Canvas.SetTop(pan_living, addLightRulesWindow.Height / 6);
            Canvas.SetLeft(pan_living, 0);
            canvas.Children.Add(pan_living);

            Canvas.SetTop(pan_bedroom, addLightRulesWindow.Height / 3);
            Canvas.SetLeft(pan_bedroom, 0);
            canvas.Children.Add(pan_bedroom);

            Canvas.SetTop(pan_bathroom, addLightRulesWindow.Height / 2);
            Canvas.SetLeft(pan_bathroom, 0);
            canvas.Children.Add(pan_bathroom);

            Canvas.SetTop(button_kitchen_unticked, 0);
            Canvas.SetLeft(button_kitchen_unticked, addLightRulesWindow.Width / 2);
            canvas.Children.Add(button_kitchen_unticked);            

            Canvas.SetTop(button_living_unticked, addLightRulesWindow.Height / 6);
            Canvas.SetLeft(button_living_unticked, addLightRulesWindow.Width / 2);
            canvas.Children.Add(button_living_unticked);

            Canvas.SetTop(button_bedroom_unticked, addLightRulesWindow.Height / 3);
            Canvas.SetLeft(button_bedroom_unticked, addLightRulesWindow.Width / 2);
            canvas.Children.Add(button_bedroom_unticked);

            Canvas.SetTop(button_bathroom_unticked, addLightRulesWindow.Height / 2);
            Canvas.SetLeft(button_bathroom_unticked, addLightRulesWindow.Width / 2);
            canvas.Children.Add(button_bathroom_unticked);

            Canvas.SetTop(button_state_off, 60);
            Canvas.SetRight(button_state_off, 0);
            canvas.Children.Add(button_state_off);

            text_time = new Text(Resources.GetFont(Resources.FontResources.NinaB), hours.ToString() + ":" + min.ToString());
            text_time.HorizontalAlignment = HorizontalAlignment.Center;
            text_time.VerticalAlignment = VerticalAlignment.Center;
            Panel pan_time = new Panel();
            pan_time.Width = addLightRulesWindow.Width / 2;
            pan_time.Height = addLightRulesWindow.Height / 6;
            pan_time.Children.Add(text_time);
            Canvas.SetTop(pan_time, 2 * addLightRulesWindow.Height / 3);
            Canvas.SetLeft(pan_time, 0);
            canvas.Children.Add(pan_time);

            Image image_up = new Image(Resources.GetBitmap(Resources.BitmapResources.up));
            image_up.HorizontalAlignment = HorizontalAlignment.Center;
            image_up.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_up = new SimpleButton(image_up, 25, 25);
            button_up.BackColor = Colors.White;
            Canvas.SetTop(button_up, 2 * addLightRulesWindow.Height / 3);
            Canvas.SetLeft(button_up, addLightRulesWindow.Width / 2);
            canvas.Children.Add(button_up);

            Image image_down = new Image(Resources.GetBitmap(Resources.BitmapResources.down));
            image_down.HorizontalAlignment = HorizontalAlignment.Center;
            image_down.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_down = new SimpleButton(image_down, 25, 25);
            button_down.BackColor = Colors.White;
            Canvas.SetTop(button_down, 2 * addLightRulesWindow.Height / 3);
            Canvas.SetLeft(button_down,3 * addLightRulesWindow.Width / 4);
            canvas.Children.Add(button_down);

            Image image_accept = new Image(Resources.GetBitmap(Resources.BitmapResources.accept));
            image_accept.HorizontalAlignment = HorizontalAlignment.Center;
            image_accept.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_accept = new SimpleButton(image_accept, addLightRulesWindow.Width / 2, addLightRulesWindow.Height / 5);
            button_accept.BackColor = Colors.White;
            Canvas.SetBottom(button_accept, 0);
            Canvas.SetLeft(button_accept, 0);
            canvas.Children.Add(button_accept);

            Image image_return = new Image(Resources.GetBitmap(Resources.BitmapResources.imreturn));
            image_return.HorizontalAlignment = HorizontalAlignment.Center;
            image_return.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_return = new SimpleButton(image_return, addLightRulesWindow.Width / 2, addLightRulesWindow.Height / 5);
            button_return.BackColor = Colors.White;
            Canvas.SetBottom(button_return, 0);
            Canvas.SetRight(button_return, 0);
            canvas.Children.Add(button_return);

            button_kitchen_ticked.Click += new EventHandler(buttonKitckenTicked_Click);
            button_kitchen_unticked.Click += new EventHandler(buttonKitckenUnticked_Click);
            button_living_ticked.Click += new EventHandler(buttonLivingTicked_Click);
            button_living_unticked.Click += new EventHandler(buttonLivingUnticked_Click);
            button_bedroom_ticked.Click += new EventHandler(buttonBedroomTicked_Click);
            button_bedroom_unticked.Click += new EventHandler(buttonBedroomUnticked_Click);
            button_bathroom_ticked.Click += new EventHandler(buttonBathroomTicked_Click);
            button_bathroom_unticked.Click += new EventHandler(buttonBathroomUnticked_Click);
            button_state_off.Click += new EventHandler(buttonStateOff_Click);
            button_state_on.Click += new EventHandler(buttonStateOn_Click);
            button_up.Click += new EventHandler(buttonUp_Click);
            button_down.Click += new EventHandler(buttonDown_Click);
            button_accept.Click += new EventHandler(buttonAccept_Click);
            button_return.Click += new EventHandler(buttonReturn_Click);

            addLightRulesWindow.Child = canvas;
            addLightRulesWindow.Visibility = Visibility.Visible;
        }                

        void buttonUp_Click(object sender, EventArgs evt)
        {
            min += 10;
            if (min == 60)
            {
                min = 0;
                hours++;
            }

            if (hours == 24)
                hours = 0;

            text_time.TextContent = hours.ToString() + ":" + min.ToString();

        }

        void buttonDown_Click(object sender, EventArgs evt)
        {
            min -= 10;
            if (min == -10)
            {
                min = 50;
                hours--;
            }
            if (hours == -1)
                hours = 23;

            text_time.TextContent = hours.ToString() + ":" + min.ToString();
        }

        void buttonKitckenTicked_Click(object sender, EventArgs evt)
        {
            kitchen = false;
            canvas.Children.Remove(button_kitchen_ticked);

            Canvas.SetTop(button_kitchen_unticked, 0);
            Canvas.SetLeft(button_kitchen_unticked, addLightRulesWindow.Width / 2);
            canvas.Children.Add(button_kitchen_unticked);
        }

        void buttonKitckenUnticked_Click(object sender, EventArgs evt)
        {
            kitchen = true;
            canvas.Children.Remove(button_kitchen_unticked);

            Canvas.SetTop(button_kitchen_ticked, 0);
            Canvas.SetLeft(button_kitchen_ticked, addLightRulesWindow.Width / 2);
            canvas.Children.Add(button_kitchen_ticked);
        }

        void buttonLivingTicked_Click(object sender, EventArgs evt)
        {
            living = false;
            canvas.Children.Remove(button_living_ticked);

            Canvas.SetTop(button_living_unticked, addLightRulesWindow.Height / 6);
            Canvas.SetLeft(button_living_unticked, addLightRulesWindow.Width / 2);
            canvas.Children.Add(button_living_unticked);
        }

        void buttonLivingUnticked_Click(object sender, EventArgs evt)
        {
            living = true;
            canvas.Children.Remove(button_living_unticked);

            Canvas.SetTop(button_living_ticked, addLightRulesWindow.Height / 6);
            Canvas.SetLeft(button_living_ticked, addLightRulesWindow.Width / 2);
            canvas.Children.Add(button_living_ticked);
        }

        void buttonBedroomTicked_Click(object sender, EventArgs evt)
        {
            bedroom = false;
            canvas.Children.Remove(button_bedroom_ticked);

            Canvas.SetTop(button_bedroom_unticked, addLightRulesWindow.Height / 3);
            Canvas.SetLeft(button_bedroom_unticked, addLightRulesWindow.Width / 2);
            canvas.Children.Add(button_bedroom_unticked);
        }

        void buttonBedroomUnticked_Click(object sender, EventArgs evt)
        {
            bedroom = true;
            canvas.Children.Remove(button_bedroom_unticked);

            Canvas.SetTop(button_bedroom_ticked, addLightRulesWindow.Height / 3);
            Canvas.SetLeft(button_bedroom_ticked, addLightRulesWindow.Width / 2);
            canvas.Children.Add(button_bedroom_ticked);
        }

        void buttonBathroomTicked_Click(object sender, EventArgs evt)
        {
            bathroom = false;
            canvas.Children.Remove(button_bathroom_ticked);

            Canvas.SetTop(button_bathroom_unticked, addLightRulesWindow.Height / 2);
            Canvas.SetLeft(button_bathroom_unticked, addLightRulesWindow.Width / 2);
            canvas.Children.Add(button_bathroom_unticked);
        }

        void buttonBathroomUnticked_Click(object sender, EventArgs evt)
        {
            bathroom = true;
            canvas.Children.Remove(button_bathroom_unticked);

            Canvas.SetTop(button_bathroom_ticked, addLightRulesWindow.Height / 2);
            Canvas.SetLeft(button_bathroom_ticked, addLightRulesWindow.Width / 2);
            canvas.Children.Add(button_bathroom_ticked);
        }

        void buttonStateOn_Click(object sender, EventArgs evt)
        {
            state = false;
            canvas.Children.Remove(button_state_on);

            Canvas.SetTop(button_state_off, 60);
            Canvas.SetRight(button_state_off, 0);
            canvas.Children.Add(button_state_off);
        }

        void buttonStateOff_Click(object sender, EventArgs evt)
        {
            state = true;
            canvas.Children.Remove(button_state_off);

            Canvas.SetTop(button_state_on, 60);
            Canvas.SetRight(button_state_on, 0);
            canvas.Children.Add(button_state_on);
        }

        void buttonAccept_Click(object sender, EventArgs evt)
        {
            prevwindow.Close();

            string rule = "";
            rule +=  Guid.NewGuid().ToString().ToUpper();
            rule += "\\" + hours.ToString() +":"+ min.ToString() + "\\";
            
            if(kitchen)
                rule+= "KITCHEN/TRUE/";
            else
                rule+= "KITCHEN/FALSE/";
            if(living)
                rule += "LIVING ROOM/TRUE/";
            else
                rule += "LIVING ROOM/FALSE/";
            if (bedroom)
                rule += "BEDROOM/TRUE/";
            else
                rule += "BEDROOM/FALSE/";
            if (bathroom)
                rule += "BATHROOM/TRUE/";
            else
                rule += "BATHROOM/FALSE";

            rule += "\\" + state.ToString().ToUpper();

            if(Drivers.getInstance().lightRules.Split('|').Length<=5)
                Drivers.getInstance().lightRules = rule;

            LightsScheduleWindow lsw = new LightsScheduleWindow(this);
            addLightRulesWindow.Close();
        }

        void buttonReturn_Click(object sender, EventArgs evt)
        {
            addLightRulesWindow.Close();
        }
    }
}
